public class MySingleList {
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public void creatList(){
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(34);
        ListNode listNode4 = new ListNode(45);
        ListNode listNode5 = new ListNode(56);

        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;
    }

    public void display(){
        ListNode cur = this.head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public boolean contain(int key){
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public int size(){
        int cnt = 0;
        ListNode cur = this.head;
        while(cur != null){
            cnt ++;
            cur = cur.next;
        }
        return cnt;
    }

    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }

    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(this.head == null){
            this.head = node;
        }else{
            ListNode cur = this.head;
            while(cur.next != null){
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    private ListNode findIndexSubOne(int index){
        ListNode cur = this.head;
        while(index - 1 != 0){
            cur = cur.next;
            index --;
        }
        return cur;
    }

    private void checkIndexAdd(int index){
        if(index < 0 || index > size()){
            throw new MySingleListIndexOutOfException("任意位置插入是index不合法");
        }
    }

    public void addIndex(int index, int data) throws MySingleListIndexOutOfException{
        checkIndexAdd(index);
        if(index == 0){
            addFirst(data);
            return;
        }
        if (index == size()){
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = findIndexSubOne(index);
        node.next = cur.next;
        cur.next = node;
    }

    public void remove(int key){
        if(this.head == null){
            System.out.println("此时链表为空，不能进行删除");
            return;
        }
        if(this.head.val == key){
            this.head = this.head.next;
            return;
        }
        ListNode cur = this.head;
        while(cur.next != null){
            if (cur.next.val == key){
                ListNode del = cur.next;
                cur.next = del.next;
                return;
            }
            cur = cur.next;
        }
    }

    public ListNode reverseList(){
        if(this.head == null){
            return null;
        }
        //只有一个头结点
        if(head.next == null){
            return head;
        }

        ListNode cur = head.next;
        head.next = null;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }

}
